import pygame
from EventManagement import EventUser
from CoreEvents import LogEvent, TickEvent, QuitRequestEvent
from TackGameEvents import KeyPressEvent, KeyDownEvent, KeyUpEvent

################################################################################
class InputManager(EventUser):
	"""
	Translates incoming events from pygame in to TackEvents. Including input
		from the Keyboard, Mouse, Joystick and the Quit Event
	"""
	############################################################################
	"""
	param - bKeyboard : (Default = True) Set True to enable Keyboard Events
	param - bMouse : (Default = False) Set True to enable Mouse Events
	param - bJoystick : (Default = False) Set True to enable Joystick Events
	"""
	def __init__(self, bKeyboard=True, bMouse=False, bJoystick=False):
		self._bKeyboard = bKeyboard
		self._bMouse = bMouse
		self._bJoystick = bJoystick
		
		# Need to know about Game Ticks
		self.RegisterEvent(TickEvent, self.Tick)
	############################################################################
	
	############################################################################
	"""
	CALLBACK(TickEvent) - Iterates through pygame event queue and translates
		in to Tack Events
	"""
	def Tick(self, rEvent):
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				self.SendEvent(QuitRequestEvent())
				continue
			if self._bKeyboard:
				# Check for Keyboard input
				bDown = None
				if event.type == pygame.KEYDOWN: bDown = True
				elif event.type == pygame.KEYUP: bDown = False
				
				sUnicode = None
				try:
					sUnicode = event.unicode
				except:
					pass
				
				if bDown is not None:
					self._KeyPress(event.key, bDown, sUnicode)
					continue
				
			if self._bMouse:
				# Check for Mouse input
				bDown = None
				if event.type == pygame.MOUSEBUTTONDOWN: bDown = True
				elif event.type == pygame.MOUSEBUTTONUP: bDown = False
				
				if bDown is not None: 
					self._MouseButtonPressed(event.button, bDown)
					continue
				
			if self._bJoystick:
				# Check for Joystick input
				pass
	############################################################################
	
	############################################################################
	def _KeyPress(self, nKey, bDown, sUniCode):
		"""
		PRIVATE - A Key has been pressed, logs it and sends a Tack Event
		
		param - nKey : Keycode
		param - bDown : True if Key is Down, False if it has been raised
		param - sUnicode : Printable keycode (if applicable)
		"""
		sButtonAction = "up"
		if bDown: sButtonAction = "down"
		sEventMessage = "Key " + sButtonAction
		if sUniCode: sEventMessage += ": " + sUniCode
		sEventMessage += " (Code '" + str(nKey) + "')"
		self.SendEvent(LogEvent({ 
			'sMessage' : sEventMessage, 
			'nLogLevel' : 0,
		}))
		
		# Send Events
		self.SendEvent(KeyPressEvent({
			'nKeyCode' : nKey,
			'bDown' : bDown,
			'sUniCode' : sUniCode,
		}))
		
		if bDown:
			self.SendEvent(KeyDownEvent({
				'nKeyCode' : nKey,
				'sUniCode' : sUniCode,
			}))
		else:
			self.SendEvent(KeyUpEvent({
				'nKeyCode' : nKey,
				'sUniCode' : sUniCode,
			}))
	############################################################################
	
	############################################################################
	def _MouseButtonPressed(self, nButton, bDown):
		"""
		PRIVATE - Mouse Button has been pressed, log it and send a Tack Event
		
		param - nButton : Mouse Button Identifier
		param - bDown : True if the button is down, False if it has been raised 
		"""
		sButtonAction = "up"
		if bDown: sButtonAction = "down"
		self.SendEvent(LogEvent({ 
			'sMessage' : "Mouse button"+ nButton + " " + sButtonAction, 
			'nLogLevel' : 0,
		}))
		
		# TODO: Send a MousePress Event
	############################################################################
################################################################################
